package com.yongcheon.sms_manager.activities;

import android.app.ActionBar;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.view.MenuItem;
import android.view.Window;

import com.yongcheon.sms_manager.R;
import com.yongcheon.sms_manager.data.preference.SpamNumberData;
import com.yongcheon.sms_manager.data.sms_spam.SMSData;
import com.yongcheon.sms_manager.data.sms_spam.SMS_Spam_Data;
import com.yongcheon.sms_manager.data.sms_spam.SpamData;
import com.yongcheon.sms_manager.database.SpamDBManager;
import com.yongcheon.sms_manager.utils.AndroidSystemUtil;
import com.yongcheon.sms_manager.utils.DefaultAppUtil;
import com.yongcheon.sms_manager.vo.SMSVO;
import com.yongcheon.sms_manager.vo.SpamNumVO;

import java.util.ArrayList;

/**
 * Created by yongcheon on 2014-12-05.
 */
public class PreferenceSettingActivity  extends PreferenceActivity{
    public static final String SPAM_SMS_BLOCK_ENABLE =  "pref_key_spam_block_enable";
    public static final String SPAM_PHONE_BLOCK_ENABLE  =  "pref_key_phone_block_enable";

    private Preference mSmsDisabledPref;
    private Preference mSmsEnabledPref;
    private PreferenceScreen prefRoot;

    @Override
    protected void onCreate(Bundle bundle) {
        setActionBar();
        super.onCreate(bundle);
        loadPrefs();
        processExtra();
    }

    public void processExtra(){
        Intent intent = getIntent();
        Bundle extra = intent.getExtras();
        if(extra != null){
            if(extra.get("menu").equals("autoRegister")){
                autoRegister();
            }
        }
    }

    public void autoRegister(){
        if(DefaultAppUtil.isWritable(this)){
            setProgressBarIndeterminateVisibility(true);

            SMSData smsData = new SMSData();
            SpamData spamData = new SpamData(this);
            SpamNumberData spamNumData = new SpamNumberData(this);
            SQLiteDatabase database = SpamDBManager.getDatabase(this);

            ArrayList<SpamNumVO> list = spamNumData.selectReturnVo(null, null, database);
            ArrayList<SMSVO> smsList = new ArrayList<SMSVO>();
            for(int i=0; i<list.size(); i++){
                SpamNumVO spamNumVO = list.get(i);
                ArrayList<SMSVO> tmpList = smsData.select(spamNumVO.getAddress(), this, null);
                for(int j=0; j<tmpList.size(); j++){
                    SMSVO tmpVo = tmpList.get(j);
                    tmpVo.setSpam_desc(spamNumVO.getSubject());
                    tmpVo.setSpam_category(spamNumVO.getCategory_2());
                }
                if(tmpList != null && tmpList.size() > 0){
                    smsList.addAll(tmpList);
                }
            }

            SMS_Spam_Data.deleteSMS_insertSpam(smsList, this, smsData, spamData);

            setProgressBarIndeterminateVisibility(false);
        }

    }

    public void setActionBar(){
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        setProgressBarIndeterminateVisibility(false);
    }

    @Override
    protected void onResume() {
        super.onResume();
        updateSmsEnabledState();
    }

    private void loadPrefs(){
        addPreferencesFromResource(R.xml.preferences);
        prefRoot = (PreferenceScreen)findPreference("pref_key_root");
        mSmsDisabledPref = findPreference("pref_key_sms_disabled");
        mSmsEnabledPref = findPreference("pref_key_sms_enabled");
        updateSmsEnabledState();
    }

    private void updateSmsEnabledState() {
        if(AndroidSystemUtil.ANDROID_VERSION <= 16) {
            prefRoot.removePreference(mSmsDisabledPref);
            prefRoot.removePreference(mSmsEnabledPref);
        }else {
            boolean isDefault = DefaultAppUtil.checkIsDefault(this);
            if (isDefault) {
                prefRoot.removePreference(mSmsDisabledPref);
                prefRoot.addPreference(mSmsEnabledPref);
            } else {
                prefRoot.addPreference(mSmsDisabledPref);
                prefRoot.removePreference(mSmsEnabledPref);
            }
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
